﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using mal.balades.Business.DataModel;
using mal.balades.Business.Log;
using mal.balades.Business.Web;

namespace mal.balades.Web
{
    public partial class _Default : PageBase
    {
        private int NumberOfPage = 4;
        private int NumberOfVisitsPerPage = 2;


        protected void Page_Load(object sender, EventArgs e)
        {
            MalLogManager.WebLogger.Info("");

            this.categories.DataSource = BaladesManager.GetCateogries().OrderBy(c => c.CategoryTypeID).ToList();
            this.categories.DataBind();

            //this.rptVisits.ItemCreated += new RepeaterItemEventHandler(rptVisits_ItemCreated);       
            this.rptVisits.ItemDataBound += new RepeaterItemEventHandler(rptVisits_ItemDataBound);

            BuildVisitsPages();

        }

        void rptVisits_ItemCreated(object sender, RepeaterItemEventArgs e)
        {

        }

        private void BuildVisitsPages()
        {
            List<int> pages = new List<int>();

            for (int i = 0; i < NumberOfPage; i++)
            {
                pages.Add(i);
            }

            this.pages.DataSource = pages;
            this.pages.DataBind();

            this.rptVisits.DataSource = pages;
            this.rptVisits.DataBind();
        }

        void rptVisits_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Repeater rptVisitsBloc = (Repeater)e.Item.FindControl("rptVisitsBloc");

                int page = (int)e.Item.DataItem;

                IEnumerable<VisitList> visits = BaladesManager.GetVisitsForHomePage();

                visits = visits.Skip(page * NumberOfVisitsPerPage).Take(NumberOfVisitsPerPage);

                rptVisitsBloc.DataSource = visits;
                rptVisitsBloc.DataBind();
            }
        }


    }
}
